package com.pearadmin.system.controller;

import com.github.pagehelper.PageInfo;
import com.pearadmin.system.domain.*;
import com.pearadmin.common.tools.string.Convert;
import com.pearadmin.common.web.base.BaseController;
import com.pearadmin.common.web.domain.request.PageDomain;
import com.pearadmin.common.web.domain.response.Result;
import com.pearadmin.common.web.domain.response.module.ResultTable;
import com.pearadmin.common.tools.secure.SecurityUtil;
import com.pearadmin.system.domain.model.ReportBaseinfoModel;
import com.pearadmin.system.mapper.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import com.pearadmin.system.service.IReportBaseinfoService;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 报告基本信息Controller
 *
 * @author ZSX
 * @date 2021-04-09
 */
@RestController
@RequestMapping("/system/baseinfo")
public class ReportBaseinfoController extends BaseController
{
    private String prefix = "system/baseinfo";

    @Autowired
    private IReportBaseinfoService reportBaseinfoService;

    @Autowired
    private ReportCourseTargetMapper reportCourseTargetMapper;

    @Autowired
    private ReportTargetObservationContactMapper reportTargetObservationContactMapper;

    @Autowired
    private ReportEvaluateAbilityRelevanceMapper reportEvaluateAbilityRelevanceMapper;

    @Autowired
    private ReportAssessmentRationalityMapper reportAssessmentRationalityMapper;

    @Autowired
    private ReportUsualScoreStandardMapper reportUsualScoreStandardMapper;

    @Autowired
    private ReportEvaluationResultsMapper reportEvaluationResultsMapper;

    @Autowired
    private TeacherAnalysisImprovementMapper teacherAnalysisImprovementMapper;

    @GetMapping("/main")
    public ModelAndView main(ModelMap mmap)
    {
        SysUser sysUser = (SysUser) SecurityUtil.currentUserObj();
        mmap.put("sysUser",sysUser);
        return jumpPage(prefix + "/main");
    }

    /**
     * 查询报告基本信息列表
     */
    @ResponseBody
    @GetMapping("/data")
    public ResultTable list(@ModelAttribute ReportBaseinfo reportBaseinfo, PageDomain pageDomain)
    {
        PageInfo<ReportBaseinfo> pageInfo = reportBaseinfoService.selectReportBaseinfoPage(reportBaseinfo,pageDomain);
        return pageTable(pageInfo.getList(),pageInfo.getTotal());
    }

    /**
     * 新增报告基本信息
     */
    @GetMapping("/add")
    public ModelAndView add()
    {
        return jumpPage(prefix + "/add");
    }

    /**
     * 新增保存报告基本信息
     */
    @ResponseBody
    @PostMapping("/save")
    public Result save(@RequestBody ReportBaseinfoModel reportBaseinfoModel)
    {
        return decide(reportBaseinfoService.save(reportBaseinfoModel));
    }

    /**
     * 修改报告基本信息
     */
    @GetMapping("/edit")
    @PreAuthorize("hasPermission('/system/baseinfo/edit','system:baseinfo:edit')")
    public ModelAndView edit(Integer id, ModelMap mmap)
    {
        ReportBaseinfoModel reportBaseinfoModel = new ReportBaseinfoModel();
        ReportBaseinfo reportBaseinfo = reportBaseinfoService.selectReportBaseinfoById(id);
        reportBaseinfoModel.setReportBaseinfo(reportBaseinfo);

        SysUser sysUser = (SysUser) SecurityUtil.currentUserObj();
        if(!sysUser.getUserId().equalsIgnoreCase(reportBaseinfo.getCreateBy())){
            return jumpPage("error/403");
        }

        ReportCourseTarget reportCourseTargetCondition = new ReportCourseTarget();
        reportCourseTargetCondition.setReportId(reportBaseinfo.getId().longValue());
        List<ReportCourseTarget> reportCourseTargetList = reportCourseTargetMapper.selectReportCourseTargetList(reportCourseTargetCondition);
        reportBaseinfoModel.setReportCourseTargetList(reportCourseTargetList);

        ReportTargetObservationContact reportTargetObservationContactCondition = new ReportTargetObservationContact();
        reportTargetObservationContactCondition.setReportId(reportBaseinfo.getId().longValue());
        List<ReportTargetObservationContact> reportTargetObservationContactList = reportTargetObservationContactMapper.selectReportTargetObservationContactList(reportTargetObservationContactCondition);
        reportBaseinfoModel.setReportTargetObservationContactList(reportTargetObservationContactList);

        ReportEvaluateAbilityRelevance reportEvaluateAbilityRelevanceCondition = new ReportEvaluateAbilityRelevance();
        reportEvaluateAbilityRelevanceCondition.setReportId(reportBaseinfo.getId().longValue());
        List<ReportEvaluateAbilityRelevance> reportEvaluateAbilityRelevanceList = reportEvaluateAbilityRelevanceMapper.selectReportEvaluateAbilityRelevanceList(reportEvaluateAbilityRelevanceCondition);
        reportBaseinfoModel.setReportEvaluateAbilityRelevanceList(reportEvaluateAbilityRelevanceList);

        ReportAssessmentRationality reportAssessmentRationality = reportAssessmentRationalityMapper.selectByReportId(reportBaseinfo.getId().longValue());
        reportBaseinfoModel.setReportAssessmentRationality(reportAssessmentRationality);

        ReportUsualScoreStandard reportUsualScoreStandardCondition = new ReportUsualScoreStandard();
        reportUsualScoreStandardCondition.setReportId(reportBaseinfo.getId().longValue());
        List<ReportUsualScoreStandard> reportUsualScoreStandardList = reportUsualScoreStandardMapper.selectReportUsualScoreStandardList(reportUsualScoreStandardCondition);
        reportBaseinfoModel.setReportUsualScoreStandardList(reportUsualScoreStandardList);

        ReportEvaluationResults reportEvaluationResultsCondition = new ReportEvaluationResults();
        reportEvaluationResultsCondition.setReportId(reportBaseinfo.getId().longValue());
        List<ReportEvaluationResults> reportEvaluationResultsList = reportEvaluationResultsMapper.selectReportEvaluationResultsList(reportEvaluationResultsCondition);
        reportBaseinfoModel.setReportEvaluationResultsList(reportEvaluationResultsList);

        TeacherAnalysisImprovement teacherAnalysisImprovement = teacherAnalysisImprovementMapper.selectByReportId(reportBaseinfo.getId().longValue());
        reportBaseinfoModel.setTeacherAnalysisImprovement(teacherAnalysisImprovement);

        mmap.put("reportBaseinfoModel", reportBaseinfoModel);
        return jumpPage(prefix + "/edit");
    }

    /**
     * 修改保存报告基本信息
     */
    @ResponseBody
    @PostMapping("/update")
    @PreAuthorize("hasPermission('/system/baseinfo/edit','system:baseinfo:edit')")
    public Result update(@RequestBody ReportBaseinfoModel reportBaseinfoModel)
    {

        return decide(reportBaseinfoService.update(reportBaseinfoModel));
    }

    /**
     * 删除报告基本信息
     */
    @ResponseBody
    @DeleteMapping( "/batchRemove")
    @PreAuthorize("hasPermission('/system/baseinfo/remove','system:baseinfo:remove')")
    public Result batchRemove(String ids)
    {
        return decide(reportBaseinfoService.deleteReportBaseinfoByIds(Convert.toStrArray(ids)));
    }

    /**
     * 删除
     */
    @ResponseBody
    @DeleteMapping("/remove/{id}")
    @PreAuthorize("hasPermission('/system/baseinfo/remove','system:baseinfo:remove')")
    public Result remove(@PathVariable("id") Integer id)
    {
        ReportBaseinfo reportBaseinfo = reportBaseinfoService.selectReportBaseinfoById(id);
        SysUser sysUser = (SysUser) SecurityUtil.currentUserObj();
        if(!sysUser.getUserId().equalsIgnoreCase(reportBaseinfo.getCreateBy())){
            return Result.failure("您无权限删除不是自己新建的报告");
        }
        return decide(reportBaseinfoService.deleteReportBaseinfoById(id));
    }
}
